/**
 * 
 */
$(function() {
	$('.ui-state-default').hover(
		function(){ $(this).addClass('ui-state-hover'); }, 
		function(){ $(this).removeClass('ui-state-hover'); }
	);
	
	$('#column_left').tree({
		data: { 
			type: 'json',
			async: true, 
			opts: {
				method: 'POST', 
				url: base_path+'backend/filemanager/directory'
			}
		},
		selected: 'top',
		ui: {
			theme_name: 'classic',
			animation: 500
		},	
		types: { 
			'default': {
				clickable: true,
				creatable: false,
				renameable: false,
				deletable: false,
				draggable: false,
				max_children: -1,
				max_depth: -1,
				valid_children: 'all'
			}
		},
		callback: {
			beforedata: function(NODE, TREE_OBJ) { 
				if (NODE == false) {
					TREE_OBJ.settings.data.opts.static = [ 
						{
							data: 'image',
							attributes: { 
								'id': 'top',
								'directory': ''
							}, 
							state: 'closed'
						}
					];
					
					return { 'directory': '' };
				} else {
					TREE_OBJ.settings.data.opts.static = false;  
					
					return { 'directory': $(NODE).attr('directory') };
				}
			},		
			onselect: function (NODE, TREE_OBJ) {
				$.ajax({
					url: base_path+'backend/filemanager/files',
					type: 'POST',
					data: 'directory='+encodeURIComponent($(NODE).attr('directory')),
					dataType: 'json',
					success: function(json) {
						html = '<div>';
						if (json) {
							for (i = 0; i < json.length; i++) {
								name = '';
								filename = json[i]['filename'];
								for (j = 0; j < filename.length; j = j+15) {
									name += filename.substr(j, 15)+'<br />';
								}
								name += json[i]['size'];
								html += '<a file="'+json[i]['file']+'"><img src="'+json[i]['thumb']+'" title="'+json[i]['filename']+'" /><br />'+name+'</a>';
							}
						}
						html += '</div>';
						$('#column_right').html(html);
					}
				});
			}
		}
	});	
	
	$('#column_right a').live('click', function () {
		if ($(this).attr('class') == 'selected') {
			$(this).removeAttr('class');
		} else {
			$('#column_right a').removeAttr('class');
			$(this).attr('class', 'selected');
		}
	});
	
	$('#create').bind('click', function () {
		var tree = $.tree.focused();
		if (tree.selected) {
			$('#dialog').remove();
			html  = '<div id="dialog">';
			html += 'Thư mục mới: <input type="text" class="input ui-corner-all" name="name" value="" /> <input type="button" value="Submit" />';
			html += '</div>';
			$('#column_right').prepend(html);
			$('input[type="button"]').button();
			
			$('#dialog').dialog({
				title: 'Tạo thư mục mới',
				resizable: false
			});	
			$('#dialog input[type=\'button\']').bind('click', function () {
				$.ajax({
					url: base_path+'backend/filemanager/create',
					type: 'POST',
					data: 'directory='+encodeURIComponent($(tree.selected).attr('directory'))+'&name='+encodeURIComponent($('#dialog input[name=\'name\']').val()),
					dataType: 'json',
					success: function(json) {
						if (json.success) {
							$('#dialog').remove();
							tree.refresh(tree.selected);
							alert(json.success);
						} else {
							alert(json.error);
						}
					}
				});
			});
		} else {
			alert('Cảnh báo: Hãy chọn một thư mục!');	
		}
	});
	
	$('#delete').bind('click', function () {
		path = $('#column_right a.selected').attr('file');
		if (path) {
			$.ajax({
				url: base_path+'backend/filemanager/delete',
				type: 'POST',
				data: 'path='+path,
				dataType: 'json',
				success: function(json) {
					if (json.success) {
						var tree = $.tree.focused();
						tree.select_branch(tree.selected);
						alert(json.success);
					}
					if (json.error) {
						alert(json.error);
					}
				}
			});				
		} else {
			var tree = $.tree.focused();
			
			if (tree.selected) {
				$.ajax({
					url: base_path+'backend/filemanager/delete',
					type: 'POST',
					data: 'path='+encodeURIComponent($(tree.selected).attr('directory')),
					dataType: 'json',
					success: function(json) {
						if (json.success) {
							tree.select_branch(tree.parent(tree.selected));
							tree.refresh(tree.selected);
							alert(json.success);
						} 
						
						if (json.error) {
							alert(json.error);
						}
					}
				});			
			} else {
				alert('Cảnh báo: Hãy chọn một tập tin hoặc thư mục!');
			}			
		}
	});
	
	$('#move').bind('click', function () {
		$('#dialog').remove();
		html  = '<div id="dialog">';
		html += 'Chuyển: <select name="to" class="input ui-corner-all"></select> <input type="button" value="Submit" />';
		html += '</div>';
		$('#column_right').prepend(html);
		$('input[type="button"]').button();
		
		$('#dialog').dialog({
			title: 'Chuyển',
			resizable: false
		});
		$('#dialog select[name=\'to\']').load(base_path+'backend/filemanager/folders');
		$('#dialog input[type=\'button\']').bind('click', function () {
			path = $('#column_right a.selected').attr('file');
			if (path) {																
				$.ajax({
					url: base_path+'backend/filemanager/move',
					type: 'POST',
					data: 'from='+encodeURIComponent(path)+'&to='+encodeURIComponent($('#dialog select[name=\'to\']').val()),
					dataType: 'json',
					success: function(json) {
						if (json.success) {
							$('#dialog').remove();
							var tree = $.tree.focused();
							tree.select_branch(tree.selected);
							alert(json.success);
						}
						if (json.error) {
							alert(json.error);
						}
					}
				});
			} else {
				var tree = $.tree.focused();
				$.ajax({
					url: base_path+'backend/filemanager/move',
					type: 'POST',
					data: 'from='+encodeURIComponent($(tree.selected).attr('directory'))+'&to='+encodeURIComponent($('#dialog select[name=\'to\']').val()),
					dataType: 'json',
					success: function(json) {
						if (json.success) {
							$('#dialog').remove();							
							tree.select_branch('#top');								
							tree.refresh(tree.selected);							
							alert(json.success);
						}												
						if (json.error) {
							alert(json.error);
						}
					}
				});				
			}
		});
	});
	$('#copy').bind('click', function () {
		$('#dialog').remove();		
		html  = '<div id="dialog">';
		html += 'Copy <input type="text" class="input ui-corner-all" name="name" value="" /> <input type="button" value="Submit" />';
		html += '</div>';
		$('#column_right').prepend(html);
		$('input[type="button"]').button();
		
		$('#dialog').dialog({
			title: 'Copy',
			resizable: false
		});		
		$('#dialog select[name=\'to\']').load(base_path+'backend/filemanager/folders');		
		$('#dialog input[type=\'button\']').bind('click', function () {
			path = $('#column_right a.selected').attr('file');							 
			if (path) {																
				$.ajax({
					url: base_path+'backend/filemanager/copy',
					type: 'POST',
					data: 'path='+encodeURIComponent(path)+'&name='+encodeURIComponent($('#dialog input[name=\'name\']').val()),
					dataType: 'json',
					success: function(json) {
						if (json.success) {
							$('#dialog').remove();							
							var tree = $.tree.focused();							
							tree.select_branch(tree.selected);							
							alert(json.success);
						}												
						if (json.error) {
							alert(json.error);
						}
					}
				});
			} else {
				var tree = $.tree.focused();				
				$.ajax({
					url: base_path+'backend/filemanager/copy',
					type: 'POST',
					data: 'path='+encodeURIComponent($(tree.selected).attr('directory'))+'&name='+encodeURIComponent($('#dialog input[name=\'name\']').val()),
					dataType: 'json',
					success: function(json) {
						if (json.success) {
							$('#dialog').remove();							
							tree.select_branch(tree.parent(tree.selected));							
							tree.refresh(tree.selected);							
							alert(json.success);
						} 												
						if (json.error) {
							alert(json.error);
						}
					}
				});				
			}
		});	
	});
	
	$('#rename').bind('click', function () {
		$('#dialog').remove();		
		html  = '<div id="dialog">';
		html += 'Đổi tên <input type="text" class="input ui-corner-all" name="name" value="" /> <input type="button" value="Submit" />';
		html += '</div>';
		$('#column_right').prepend(html);
		$('input[type="button"]').button();
		
		$('#dialog').dialog({
			title: 'Đổi tên',
			resizable: false
		});
		
		$('#dialog input[type=\'button\']').bind('click', function () {
			path = $('#column_right a.selected').attr('file');							 
			if (path) {		
				$.ajax({
					url: base_path+'backend/filemanager/rename',
					type: 'POST',
					data: 'path='+encodeURIComponent(path)+'&name='+encodeURIComponent($('#dialog input[name=\'name\']').val()),
					dataType: 'json',
					success: function(json) {
						if (json.success) {
							$('#dialog').remove();							
							var tree = $.tree.focused();					
							tree.select_branch(tree.selected);							
							alert(json.success);
						} 						
						if (json.error) {
							alert(json.error);
						}
					}
				});			
			} else {
				var tree = $.tree.focused();				
				$.ajax({ 
					url: base_path+'backend/filemanager/rename',
					type: 'POST',
					data: 'path='+encodeURIComponent($(tree.selected).attr('directory'))+'&name='+encodeURIComponent($('#dialog input[name=\'name\']').val()),
					dataType: 'json',
					success: function(json) {
						if (json.success) {
							$('#dialog').remove();								
							tree.select_branch(tree.parent(tree.selected));							
							tree.refresh(tree.selected);							
							alert(json.success);
						} 						
						if (json.error) {
							alert(json.error);
						}
					}
				});
			}
		});		
	});
	
	new AjaxUpload('#upload', {
		action: base_path+'backend/filemanager/upload',
		name: 'image',
		autoSubmit: false,
		responseType: 'json',
		onChange: function(file, extension) {
			var tree = $.tree.focused();			
			if (tree.selected) {
				this.setData({'directory': $(tree.selected).attr('directory')});
			} else {
				this.setData({'directory': ''});
			}			
			this.submit();
		},
		onSubmit: function(file, extension) {
			$('#upload').append('<img src="'+base_path+'assets/img/loading.gif" id="loading" style="padding-left: 5px;" />');
		},
		onComplete: function(file, json) {
			if (json.success) {
				var tree = $.tree.focused();					
				tree.select_branch(tree.selected);				
				alert(json.success);
			}			
			if (json.error) {
				alert(json.error);
			}
			
			$('#loading').remove();	
		}
	});
		
	$('#refresh').bind('click', function () {
		var tree = $.tree.focused();
		tree.refresh(tree.selected);
	});	
});